import time

from selenium import webdriver
import os
import pytest
import allure
from page.agent import Agent
from selenium.webdriver.common.by import By
from tools.tools import Tools

@allure.feature('代理商模块')
class TestAgent:
    @pytest.fixture(scope='class',autouse=True)
    def classsetup(self):
        """
        类级别初始化
        :return:
        """
        yield

    @pytest.fixture(autouse=True)
    def methodestupteardown(self):
        """
        方法初始化
        :return:
        """
        self.driver = webdriver.Edge()
        self.driver.maximize_window()
        self.po = Agent(self.driver)
        self.logger = Tools().get_logger("Test.Agent")
        self._forward()
        yield
        time.sleep(2)
        self.driver.quit()


    def _forward(self):
        """登录到代理商的公共操作"""
        with allure.step('登录'):
            self.logger.info('执行前置操作：登录')
            self.po.login_success()
            dy = self.po.find_element((By.XPATH,'/html/body/div/div/div[1]/div/div[2]/ul/li[2]')).text
            assert '广告营销' == dy,f'断言失败,实际值{dy}'
            self.logger.info(f'登录成功:{dy}')

        with allure.step('进入代理商'):
            self.logger.info('代理商')
            self.po.show_play_manage()
            dy2 = self.po.find_element((By.XPATH,'/html/body/div/div/div[2]/div/div[2]/div[1]/div[1]/div[2]')).text
            assert '新增代理商' == dy2,f'断言失败,实际值{dy2}'
            self.logger.info(f'进入代理商成功{dy2}')

    @allure.story('新增代理商操作')
    def test_new_agent(self):
        """新增代理"""
        with allure.step('输入用户名'):
            self.po.new_Agent()
            dy = self.po.find_element((By.XPATH,'/html/body/div[3]/p')).text
            assert '用户名已存在' == dy
            self.logger.info(f'新增代理成功{dy}')

if __name__ == '__main__':
    #生成Allure原始数据
    pytest.main(['./Case','-v','--alluredir=./report/allure_raw'])

    #生成HTML报告
    os.system('allure generate ./report/allure_raw -o ./report/allure_html --clean')
    os.system('allure open ./report/allure_html')